#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define Width 640
#define Height 480
void main()
{
	srand((unsigned)time(NULL));
	initgraph(Width, Height);
	int ball_x[10], ball_y[10];
	int ball_vx[10], ball_vy[10];
	int radius = 20;
	for (int i = 0; i < 10; i++) 
	{
		ball_x[i] = rand() % 640;
		ball_y[i] = rand() % 480;
		ball_vx[i] =1;
		ball_vy[i] =1;
	}
	while (1) 
	{
		for (int i = 0; i < 10; i++) 
		{
			setcolor(BLACK);
			setfillcolor(BLACK);
			fillcircle(ball_x[i], ball_y[i], radius);
			setcolor(BLACK); 
			//setlinecolor(BLACK);
			//line(ball_x[i], ball_y[i], ball_x[i+1], ball_y[i+1]);
			ball_x[i] += ball_vx[i];
			ball_y[i] += ball_vy[i];
			if (ball_y[i] <= radius || ball_y[i] >= Height - radius) 
			{
				ball_vy[i] = -ball_vy[i];
			}
			if (ball_x[i] <= radius || (ball_x[i] >= Width - radius))
			{
				ball_vx[i] = -ball_vx[i];
			}
			for (int j = 0; j < 10; j++) 
			{
				if((ball_x[i]-ball_x[j])*(ball_x[i]-ball_x[j])+(ball_y[i]-ball_y[j])*(ball_y[i]-ball_y[j])<=(radius+radius)*(radius+radius))
				{
					ball_vy[i] = -ball_vy[i];
					ball_vx[i] = -ball_vx[i];
					ball_vy[j] = -ball_vy[j];
					ball_vx[j] = -ball_vx[j];
				}
			}
			setcolor(WHITE);
			setfillcolor(RED);
			fillcircle(ball_x[i], ball_y[i], radius);
			//setlinecolor(BLUE);
			//line(ball_x[i], ball_y[i], ball_x[i + 1], ball_y[i + 1]);
		}
		Sleep(10);
		FlushBatchDraw();
	}
	_getch();
	closegraph();
}
